angular.module('appointment', [])
    .controller('appointmentCtrl', ['$scope', 'httpService', 'nextPageByRole', '$rootScope', '$localStorage', 'alertmsg', '$timeout', '$interval', function($scope, httpService, nextPageByRole, $rootScope, $localStorage, alertmsg, $timeout, $interval) {
        $scope.data = angular.copy($scope.service);

        $scope.user = $localStorage.user;

        if($scope.data) {
            if($scope.data.province) {
                $scope.data.province = $scope.data.province.name.replace(/\s+/g,"");
                $scope.data.city = $scope.data.city.name.replace(/\s+/g,"");
                $scope.data.area = $scope.data.area.replace(/\s+/g,"");
            } else {
                window.history.go(-2);
            }
        }
        $scope.data.public_order = true;

        // 设定师傅
        $scope.setMaster = function(master) {
//          $scope.data.facilitator_id = master.id;
            $scope.data.customer_service_id = master.id;
            $scope.master = master;
            $scope.masters = null;
            $scope.keyword = '';
            $scope.data.facilitator_id_value = null;
            $('#servermodal').modal('hide')
        };
        
        $scope.cleanMaster = function() {
          $scope.master = null;
//        $scope.data.facilitator_id = null;
          $scope.data.customer_service_id = null;
          // console.log($scope.master)
        }

        function showError(msg) {
            $scope.noperson = msg;
            $timeout(function() {
                $scope.noperson = '';
            }, 1500);
        }

        // 搜索师父
        $scope.getMaster = function(keyword) {
            var first = 1;
            $scope.noperson = '';
            if(keyword && first == 1) {
                ++first;
                httpService.query('web/users/fetch_master', { keyword: keyword, page: 1, limit: limit}).then(function(res) {
                    first == 1;
                    if(res.code == 200) {
                        $scope.masters = res.users;
                        if($scope.masters.length == 0) {
                            showError('未搜索到师傅，请重新输入关键字！');
                        }
                    } else {
                        alertmsg(res.msg);
                    }
                }, function() {
                    first == 1
                });
            } else {
                first == 1
                showError('请输入关键字!');
            }
        };

        var timer;
        function waitGetMaster() {
            timer = $interval(function() {
                $scope.getMaster($scope.keyword);
                $interval.cancel(timer);
            }, 800);
        }

        $scope.$watch('keyword', function(newData, oldData) {
            if(newData) {
                $interval.cancel(timer);
                waitGetMaster();
            }
        });

        $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
            if (toState.name == 'reservation.appointment') {
                $rootScope.step = 3;
            }
        });

        // 获取估价
        function getPrice() {
            httpService.query('web/orders/cal_price', $scope.data, 'post').then(function(res) {
                if (res.code == 200) {
                    $scope.data.total = res.price;
                }
            });
        }
        getPrice();

        // 立即预约
        var firstsubmit = true;
        $scope.getAppoint = function() {
            if (firstsubmit) {
                firstsubmit = false;
                httpService.query('web/orders/appoint', $scope.data, 'post').then(function(res) {
                    firstsubmit = true;
                    if (res.code == 200) {
                        alertmsg('预定服务成功！');
                        $scope.service = {};
                        $localStorage.data_save = '';
                        if (!$localStorage.mutian_token) {
                            $rootScope.token = res.token;
                            $rootScope.user = $localStorage.user = user = res.user;
                            $rootScope.user_role = res.user.role;
                            $localStorage.mutian_token = res.token;
                        }
                        nextPageByRole.go(res.user.role);
                    } else {
                        alertmsg(res.msg);
                    }
                }, function() {
                    firstsubmit = true;
                });
            } else {
                alertmsg('请不要多次提交!');
            }
        };

        $scope.goBackInformatgion = function() {
            window.history.go(-1);
        };


    }]);
